<template>
  <el-dialog
      title="添加/修改设备组"
      :visible.sync="visible"
      :id.sync="id"
      @opened="openDialog"
      :before-close="closeDialog"
      width="480px">
    <el-form label-width="80px">
      <el-form-item label="设备组名称" prop="deviceGroup.deviceGroupName" size="small">
        <el-input size="small" v-model="deviceGroup.deviceGroupName"></el-input>
      </el-form-item>

      <el-form-item align="center" style="margin-left: -80px;">
        <el-button type="primary" size="small" plain @click="handleUpdate" v-if="id!=''">修改设备</el-button>
        <el-button type="primary" size="small" plain @click="onSubmit" v-else>添加设备</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import {getDeviceGroupById, updateDeviceGroup, createDeviceGroup} from "@/api/deviceGroup";

export default {
  name: 'DeviceGroupEditDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      deviceGroupType: [
        {id: 1, name: '灯光'},
        {id: 2, name: '电源'},
        {id: 3, name: 'USB'},
        {id: 4, name: '人体传感器'},
        {id: 5, name: '其他'}
      ],
      accessMode: [
        {id: 1, name: 'Home Assistant'},
        {id: 2, name: 'MQTT'}
      ],
      deviceGroup: {}
    }
  },
  methods: {

    // 添加设备信息
    onSubmit() {
      createDeviceGroup({
        'deviceGroupName': this.deviceGroup.deviceGroupName,
        'deviceGroupCode': this.deviceGroup.deviceGroupCode,
        'deviceGroupType': this.deviceGroup.deviceGroupType,
        'accessMode': this.deviceGroup.accessMode,
        'ip': this.deviceGroup.ip,
        'entityId': this.deviceGroup.entityId,
      }).then((response) => {
        if (response.data.code === 200) {
          this.$parent.fetchData()
          this.$alert('提交成功', '提示', {
            confirmButtonText: '确定'
          })

        } else {
          this.$alert('提交失败', '提示', {
            confirmButtonText: '确定'
          })
        }
      })
      this.$emit('update:visible', false)

    },

    // 更新设备信息
    handleUpdate() {
      updateDeviceGroup({
        'id': this.deviceGroup.id,
        'deviceGroupName': this.deviceGroup.deviceGroupName,
        'deviceGroupCode': this.deviceGroup.deviceGroupCode,
        'deviceGroupType': this.deviceGroup.deviceGroupType,
        'accessMode': this.deviceGroup.accessMode,
        'ip': this.deviceGroup.ip,
        'entityId': this.deviceGroup.entityId,
      }).then((response) => {
        if (response.data.code === 200) {
          this.$parent.fetchData()
          this.$alert('提交成功', '提示', {
            confirmButtonText: '确定'
          })

        } else {
          this.$alert('提交失败', '提示', {
            confirmButtonText: '确定'
          })
        }
      })
      this.$emit('update:visible', false)

    },

    // 打开对话框
    openDialog() {
      if (this.id !== '') {
        getDeviceGroupById(this.id).then((response) => {
          this.deviceGroup = response.data.data;
          delete this.deviceGroup.createTime;
        })
      }
    },

    // 关闭对话框
    closeDialog() {
      this.deviceGroup = {}
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style scoped>

</style>
